/**
 * 
 */
package neptune.web.client.view.components.editor;

import neptune.web.client.model.topology.SerializableNode;
import neptune.web.client.model.topology.SerializableSoftware;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.TreeItem;

/**
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class AddSoftwareCommand implements Command {

	private SerializableNode node;
	private TreeItem item;

	public AddSoftwareCommand(SerializableNode node, TreeItem item) {
		this.node = node;
		this.item = item;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.Command#execute()
	 */
	public void execute() {
		final SerializableSoftware soft = new SerializableSoftware();
		soft.setId("UNDEFINED");
		node.getInstalledSoftwares().add(soft);

		final TreeItem softItem = new TreeItem();

		if (item.getText().equals("Softwares"))
			item.addItem(softItem);
		else
			item.getChild(1).addItem(softItem);

		softItem.setWidget(new SoftwareMenuBar(soft, node, softItem));

		SoftwarePropsDialog dialog = new SoftwarePropsDialog(soft, softItem);
		dialog.setCancelCallback(new CancelCallBack() {
			public void onCancel() {
				softItem.remove();
				node.getInstalledSoftwares().remove(soft);
			}
		});
		dialog.center();
	}

}
